
class CalculateView extends BaseEuiView {
    public questionLabel: eui.Label;
    public answer1: eui.Button;
    public answer2: eui.Button;
    public answer3: eui.Button;
    public answer4: eui.Button;

    public showOff: egret.tween.TweenGroup;
    public showOn: egret.tween.TweenGroup;

    private question: Array<number> = [0, 0, 0, 0];
    private answer: number;

    public constructor(controller: BaseController, parent: eui.Group) {
        super(controller, parent)
        this.width = parent.width
        this.height = parent.height
        this.skinName = "resource/skins/games/CalculateSkin.exml"

        this.answer1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnTouchHandler, this);
        this.answer2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnTouchHandler, this);
        this.answer3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnTouchHandler, this);
        this.answer4.addEventListener(egret.TouchEvent.TOUCH_TAP, this.btnTouchHandler, this);

        this.showOff.addEventListener('itemComplete', this.onTweenItemComplete, this);

        this.showOff.addEventListener('complete', this.onTweenGroupComplete, this);
        this.showOn.addEventListener('complete', this.onTweenGroupComplete, this);

        this.beginQuestion();
    }

    private beginQuestion(): void {

        var a: number = Math.floor(Math.random() * 50);
        var b: number = Math.floor(Math.random() * 50);
        var p: number = Math.floor(Math.random() * 2);
        var c: number = 0;
        switch (p) {
            case 0:
                this.questionLabel.text = a + " + " + b + " = ";
                c = a + b;
                break;
            case 1:
                if (a < b) {
                    var tmp = a;
                    a = b;
                    b = tmp;
                }
                this.questionLabel.text = a + " - " + b + " = ";
                c = a - b;
                break;
            case 2:
                this.questionLabel.text = a + " * " + b + " = ";
                c = a * b;
                break;
            case 3:
                this.questionLabel.text = a + " / " + b + " = ";
                c = a / b;
                break;
        }
        this.questionLabel.text += " ?"

        this.answer = c;
        var m: { [index: number]: number; } = {};
        this.question[0] = this.answer;
        m[this.answer] = 1

        c = Math.floor(Math.random() * 50);
        while (c in m) {
            c = Math.floor(Math.random() * 50);
        }
        this.question[1] = c;
        m[c] = 1;

        c = Math.floor(Math.random() * 50);
        while (c in m) {
            c = Math.floor(Math.random() * 50);
        }
        this.question[2] = c;
        m[c] = 1;

        c = Math.floor(Math.random() * 50);
        while (c in m) {
            c = Math.floor(Math.random() * 50);
        }
        this.question[3] = c;
        m[c] = 1;

        console.log("question:" + this.question + "  answer:" + this.answer);
        this.answer1.currentState = "up"
        this.answer1.label = String(this.question[0]);
        this.answer2.label = String(this.question[1]);
        this.answer3.label = String(this.question[2]);
        this.answer4.label = String(this.question[3]);
    }

    private btnTouchHandler(event: egret.TouchEvent): void {
        console.log(event);
        if (event.target === this.answer1) {
            this.setAnswer(0);
        } else if (event.target === this.answer2) {
            this.setAnswer(1);
        } else if (event.target === this.answer3) {
            this.setAnswer(2);
        } else if (event.target === this.answer4) {
            this.setAnswer(3);
        }
    }

    private setAnswer(answer: number) {
        console.log(answer);
        if (this.question[answer] == this.answer) {
            console.log("答对了=====================");
        } else {
            console.log("答错了*********************");
        }
        this.showOff.play();
    }

    private onTweenGroupComplete(event: egret.Event): void {
        //console.log('TweenGroup play completed. +', event);
        if (event.target == this.showOff) {
            this.showOff.stop();
            this.showOn.play();
            this.beginQuestion();
        } else if (event.target == this.showOn) {
            this.showOn.stop();
        }
    }

    private onTweenItemComplete(event: egret.Event): void {
        //console.log('onTweenItemComplete. +', event);
    }
}